
#include "coloring.h"

static HWND hColorSet;		// The set color check box
static HWND hGourand;		// The Gourand Shading option
static HWND hColortheme;	// The combo box to choose the color theme

static HWND hSetColor;		// The set color button
static HWND hDisplayColor;	// The displayed color static

static int colortheme_combo[5];

void AbleSetColorBox (BOOL flag)
{
	EnableWindow (hSetColor, flag);
	EnableWindow (hDisplayColor, flag);
	EnableWindow (hColortheme, !flag);
}

	// Popup the color select dialog box and assign the value of
	// the color with it
int ChooseNewColor (HWND hWnd)
{
	DWORD dwColor, crColor;
	DWORD dwCustClrs [16];
	BOOL fSetColor = FALSE;
	int i;
	CHOOSECOLOR chsclr;

	for (i=0; i<16; i++)
		dwCustClrs[i] = RGB (255, 255, 255);
	dwColor = RGB((int)(rcolor*255.0),(int)(gcolor*255.0),(int)(bcolor*255.0));

	memset (&chsclr, 0, sizeof(chsclr));
	chsclr.lStructSize = sizeof (CHOOSECOLOR);
	chsclr.hwndOwner = hWnd;
	chsclr.hInstance = (HWND)hMainInstance;
	chsclr.rgbResult = dwColor;
	chsclr.lpCustColors = (LPDWORD)dwCustClrs;
	chsclr.lCustData = 0L;
	chsclr.Flags = CC_FULLOPEN | CC_RGBINIT;
	
	if (fSetColor = ChooseColor (&chsclr))
	{
		crColor = chsclr.rgbResult;
		
		rcolor = (float)((float)GetRValue(crColor)/255.0);
		gcolor = (float)((float)GetGValue(crColor)/255.0);
		bcolor = (float)((float)GetBValue(crColor)/255.0);
		
		return 1;
	}
	else
	{
		//ProcessCDError (CommDlgExtendedError(), hWnd);
		return 0;
	}
}

static int AddComboEntry (HWND hCombo, UINT rID)
{
	char cstr[256];

	LoadString (hMainInstance, rID, cstr, sizeof(cstr));
	return SendMessage (hCombo, CB_ADDSTRING, 0, 
			(LPARAM)(LPCSTR)cstr);
}

static void AddColorthemeEntries (HWND hCombo)
{
	colortheme_combo[0] = AddComboEntry (hCombo, IDS_HSVACROSS);
	colortheme_combo[1] = AddComboEntry (hCombo, IDS_HSVUPDOWN);
	colortheme_combo[2] = AddComboEntry (hCombo, IDS_PURPLERED);
	colortheme_combo[3] = AddComboEntry (hCombo, IDS_REDANDBLUE);
	colortheme_combo[4] = AddComboEntry (hCombo, IDS_RGB);

	SendMessage (hCombo, CB_SETCURSEL, 
		(WPARAM)colortheme_combo[colortheme], 0);
}

BOOL APIENTRY ColoringConfigureDialog (HWND hDlg, 
	 UINT message, WPARAM wParam, LPARAM lParam)
{
	LPDRAWITEMSTRUCT di;
	HBRUSH br, obr;
	HPEN pn, opn;
	COLORREF curcol;

	DWORD dlgcol = GetSysColor (COLOR_3DFACE);
	DWORD light = GetSysColor (COLOR_3DHILIGHT);
	DWORD shadow = GetSysColor (COLOR_3DSHADOW);

	switch (message)
	{
	case WM_INITDIALOG:
		hGourand = GetDlgItem (hDlg, IDC_GOURAND);
		hColortheme = GetDlgItem (hDlg, IDC_COLORTHEME);
		hColorSet = GetDlgItem (hDlg, IDC_COLORSET);

		hSetColor = GetDlgItem (hDlg, IDC_SETCOLOR);
		hDisplayColor = GetDlgItem (hDlg, IDC_SHOWSETCOLOR);

		AddColorthemeEntries (hColortheme);

		return TRUE;
		break;

	case WM_DRAWITEM:
		di = (LPDRAWITEMSTRUCT)lParam;
		if (di->CtlID == IDC_SHOWSETCOLOR)
		{
				// make colorref object of the current color
			curcol = RGB((int)(rcolor*255.0),(int)(gcolor*255.0),(int)(bcolor*255.0));
			
			if (di->itemState == ODS_DISABLED)
			{
				curcol = dlgcol;
			}
			
			br = CreateSolidBrush (curcol);
			pn = CreatePen (PS_SOLID, 1, curcol);

				// Select the object into the control's DC
			obr = (HBRUSH)SelectObject (di->hDC, br);
			opn = (HPEN)SelectObject (di->hDC, pn);

				// draw the color rectangle
			Rectangle (di->hDC, di->rcItem.left, di->rcItem.top, 
				di->rcItem.right, di->rcItem.bottom);

				// Draw the light lines first
			DeleteObject (pn);
			pn = CreatePen (PS_SOLID, 1, light);
			SelectObject (di->hDC, pn);
			MoveToEx (di->hDC, di->rcItem.right-1, di->rcItem.top, NULL);
			LineTo (di->hDC, di->rcItem.right-1, di->rcItem.bottom);
			MoveToEx (di->hDC, di->rcItem.left, di->rcItem.bottom-1, NULL);
			LineTo (di->hDC, di->rcItem.right-1, di->rcItem.bottom-1);

				// Draw the dark lines
			DeleteObject (pn);
			pn = CreatePen (PS_SOLID, 1, shadow);
			SelectObject (di->hDC, pn);
			MoveToEx (di->hDC, di->rcItem.left, di->rcItem.top, NULL);
			LineTo (di->hDC, di->rcItem.left, di->rcItem.bottom);
			MoveToEx (di->hDC, di->rcItem.left, di->rcItem.top, NULL);
			LineTo (di->hDC, di->rcItem.right-1, di->rcItem.top);

				// Select the old objects back into their place
			SelectObject (di->hDC, obr);
			SelectObject (di->hDC, opn);

				// Delete the created pens
			DeleteObject (br);
			DeleteObject (pn);
		}

		return TRUE;
		break;

	case WM_NOTIFY:
		switch (( (NMHDR*)lParam ) -> code)
		{
		case PSN_SETACTIVE:
			SendMessage (hColorSet, BM_SETCHECK, BST_UNCHECKED, 0);
			SendMessage (hGourand, BM_SETCHECK, BST_UNCHECKED, 0);

			if (coloring_mode == 0)
			{
				SendMessage (hGourand, BM_SETCHECK, BST_CHECKED, 0);
				AbleSetColorBox (FALSE);
			}
			else if (coloring_mode == 1)
			{
				SendMessage (hColorSet, BM_SETCHECK, BST_CHECKED, 0);
				AbleSetColorBox (TRUE);
			}
			
			break;

		case PSN_APPLY:
			SaveColoringSettings();
			break;

		case PSN_KILLACTIVE:
			break;
		}

		return TRUE;
		break;

	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDC_SETCOLOR:
			ChooseNewColor (hDlg);
			RedrawWindow (hDisplayColor, NULL, NULL, RDW_INVALIDATE);
			break;
		case IDC_GOURAND:
			switch (HIWORD(wParam))
			{
			case BN_CLICKED:
				coloring_mode = 0;
				AbleSetColorBox (FALSE);
				break;
			}
			break;
		case IDC_COLORTHEME:
			switch (HIWORD(wParam))
			{
			case CBN_SELCHANGE:
				colortheme = SendMessage (hColortheme, CB_GETCURSEL, 0, 0);
				break;
			}
			break;
		case IDC_COLORSET:
			switch (HIWORD(wParam))
			{
			case BN_CLICKED:
				coloring_mode = 1;
				AbleSetColorBox (TRUE);
				break;
			}
			break;
		}

		return TRUE;
		break;
	}

	return FALSE;
}